package com.itheima.project.face.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.constant.security.CustomerCacheConstant;
import com.itheima.project.constant.security.ResourceCacheConstant;
import com.itheima.project.core.enums.security.ResourceEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.ResourceFace;
import com.itheima.project.pojo.Resource;
import com.itheima.project.service.IResourceService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.basic.TreeVo;
import com.itheima.project.vo.security.ResourceVo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName ResourceFaceImpl.java
 * @Description 资源服务实现
 */
@Slf4j
@Component
public class ResourceFaceImpl implements ResourceFace {

    @Autowired
    IResourceService resourceService;

    @Override
    @Cacheable(value = ResourceCacheConstant.PAGE,key ="#pageNum+'-'+#pageSize+'-'+#resourceVo.hashCode()")
    public Page<ResourceVo> findResourceVoPage(ResourceVo resourceVo,
                                               int pageNum,
                                               int pageSize) throws ProjectException {
        try {
            Page<Resource> page = resourceService.findResourceVoPage(resourceVo, pageNum, pageSize);
            Page<ResourceVo> pageVo = BeanConv.toPage(page,ResourceVo.class);
            return pageVo;
        } catch (Exception e) {
            log.error("查询资源列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ResourceEnum.PAGE_FAIL);
        }
    }

    @Override
    @CachePut(value =ResourceCacheConstant.TREE,key = "#result.hashCode()")
    public TreeVo initResourceTreeVo(String[] checkedIds)throws ProjectException {
        try {
            return resourceService.initResourceTreeVo(checkedIds);
        } catch (Exception e) {
            log.error("查询资源数异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ResourceEnum.INIT_TREE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = ResourceCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ResourceCacheConstant.TREE,allEntries = true),
        @CacheEvict(value = CustomerCacheConstant.RESOURCES,allEntries = true),
        @CacheEvict(value = ResourceCacheConstant.MENU,allEntries = true)},
        put={@CachePut(value =ResourceCacheConstant.BASIC,key = "#result.id")})
    public ResourceVo createResource(ResourceVo resourceVo)throws ProjectException {
        try {
            return BeanConv.toBean(resourceService.createResource(resourceVo),ResourceVo.class);
        } catch (Exception e) {
            log.error("保存资源异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ResourceEnum.CREATE_FAIL);
        }

    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = ResourceCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ResourceCacheConstant.TREE,allEntries = true),
        @CacheEvict(value = CustomerCacheConstant.RESOURCES,allEntries = true),
        @CacheEvict(value = ResourceCacheConstant.MENU,allEntries = true),
        @CacheEvict(value = ResourceCacheConstant.BASIC,key = "#resourceVo.id")})
    public ResourceVo updateResource(ResourceVo resourceVo)throws ProjectException {
        try {
            Boolean flag = resourceService.updateResource(resourceVo);
            if (flag){
                return resourceVo;
            }else {
                log.error("修改资源异常!");
                throw new ProjectException(ResourceEnum.UPDATE_FAIL);
            }
        } catch (Exception e) {
            log.error("保存资源异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ResourceEnum.UPDATE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = ResourceCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ResourceCacheConstant.TREE,allEntries = true),
        @CacheEvict(value = CustomerCacheConstant.RESOURCES,allEntries = true),
        @CacheEvict(value = ResourceCacheConstant.MENU,allEntries = true)})
    public Boolean deleteResource(String[] checkedIds) throws ProjectException{
        try {
            return resourceService.deleteResource(checkedIds);
        } catch (Exception e) {
            log.error("删除资源异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ResourceEnum.DELETE_FAIL);
        }

    }
}
